package com.example.myproject.web;

import com.example.myproject.shiro.ShiroUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.concurrent.Executor;

/**
 * Created by xi on 2017/9/21.
 */
@RestController
public class LoginController {


    //@Autowired
    private Executor executor;

    /**
     * 默认也会走shirogiler但是不认走认证过滤器；userFilter
     * @return
     */
    @RequestMapping("/loginTest")
    public String loginTest() {
        Subject subject = SecurityUtils.getSubject();
        System.out.println("threadname:"+Thread.currentThread().getName()+",subject:"+subject);

        Object user = subject.getPrincipal();
        System.out.println("loginTest"+user);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                Thread t = Thread.currentThread();
                System.out.println("tname:"+t.getName());
                System.out.println("thread,subject:"+SecurityUtils.getSubject());
                Object object = SecurityUtils.getSubject().getPrincipal();
                System.out.println("thread  user:"+object);
                if(object!=null){
                    ShiroUser user = (ShiroUser)object;
                    System.out.println("thread  user name:"+user.getUserName());
                }
            }
        });
        return "loginTest";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }
    @RequestMapping("/loginUser")
    public String loginUser(String username,String password,HttpSession session) {
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();
        System.out.println("threadName;"+Thread.currentThread().getName()+",subject==="+subject+",session:"+subject.getSession(false).getId());
        try {
            subject.login(usernamePasswordToken);   //完成登录
            System.out.println("login finish,session:"+subject.getSession(false));

            Object user = subject.getPrincipal();

            System.out.println("user==" + user);

            session.setAttribute("user", user);

            return "1";
        } catch(Exception e) {
            e.printStackTrace();
            return "login";//返回登录页面
        }
    }
    @RequestMapping("/testHttpThreadSession")
    public String testHttpThreadSession(){
        executor.execute(new Runnable() {
            @Override
            public void run() {
                //这里就绑定了session到用户这里了，根据sessionId取用户数据
                Subject subject = SecurityUtils.getSubject();
                Session session = subject.getSession();
                Object object = subject .getPrincipal();
                System.out.println("1111default user:"+object);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        return "2";
    }

    @RequestMapping("/logOut")
    public String logOut(HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
//        session.removeAttribute("user");
        return "login";
    }
}